package com.jeeplus.test.jlnu.changeStuInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.changeStuInfo.domain.entity.ChangeStuInfo;
import com.jeeplus.test.jlnu.changeStuInfo.domain.entity.ChangeStuInfoHistory;
import com.jeeplus.test.jlnu.changeStuInfo.domain.vo.ChangeStuInfoReq;
import com.jeeplus.test.jlnu.changeStuInfo.mapper.ChangeStuInfoDetailMapper;
import com.jeeplus.test.jlnu.changeStuInfo.service.ChangeStuInfoDetailService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

@Service
public class ChangeStuInfoDetailServiceImpl implements ChangeStuInfoDetailService {

    @Autowired
    ChangeStuInfoDetailMapper changeStuInfoMapper;

    @Override
     public Page<Map<String, Object>> getChangeStu(ChangeStuInfoReq changeStuInfo, String officeId, Page<ChangeStuInfo> page) {
        String type = changeStuInfoMapper.getTypeById(changeStuInfo.getOfficeId());
        if(type.equals("1")||type.equals("2")||type.equals("3")){
            changeStuInfo.setSource(type);
        }else if(type.equals("4")||type.equals("6")){
            officeId="";
        }
        String transferState = String.valueOf(changeStuInfo.getTransferState());
        if (StringUtils.isNotEmpty(transferState)) {
            String[] arr = transferState.split(",");
            List<String> list = Arrays.asList(arr);
            changeStuInfo.setTransferStateList(list);
        }
        Page<ChangeStuInfo> changeStuInfoPage=changeStuInfoMapper.selectPage(changeStuInfo, officeId, page);

        List<ChangeStuInfo> records = changeStuInfoPage.getRecords();
        List<Map<String, Object>> changeStuInfoRecords = new ArrayList<>();
        for (ChangeStuInfo record : records) {
            Map<String, Object> recordMap = new HashMap<>();

            // 3. 将 AdmissionInfoRespVo 中的字段放入 map
            Field[] fields = ChangeStuInfo.class.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    // 获取字段值并添加到 map 中
                    Object value = field.get(record);


                    recordMap.put(field.getName(), value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            // 5. 添加处理后的记录
            changeStuInfoRecords.add(recordMap);
        }


        Page<Map<String, Object>> modifiedPage = new Page<>(changeStuInfoPage.getCurrent(), changeStuInfoPage.getSize(), changeStuInfoPage.getTotal());
        modifiedPage.setRecords(changeStuInfoRecords);
        return modifiedPage;
    }
    @Override
    public List<ChangeStuInfoHistory>  getChangeStu(Long infoId) {
        return changeStuInfoMapper.getByInfoId(infoId);
    }

    @Override
    public int batchUpdateChangeInfo(ChangeStuInfo changeStuInfo) {
        String transferState = String.valueOf(changeStuInfo.getTransferState());
        return changeStuInfoMapper.batchUpdateChangeInfo(changeStuInfo.getIds(),transferState);
    }

}
